<template>
  <div>
    <div class="clearfix">
      <el-button
        size="mini"
        class="float-right"
        type="primary"
        @click="addBtnClick">
        <i class="el-icon-plus"/>
      </el-button>
    </div>
    <br>
    <!--列表数据-->
    <w-cell
      v-for="(v, k) in dataArr"
      :key="k"
      @editClick="editBtnClick(v)"
      @removeClick="detailBtnClick(v)"
      :hover="false"
      :title="v.title"
      :describe="`${v.index}`"
      :detail="v.detail"
      :operation="true"
      :icon="v.mAdminBannerListUrl"/>
    <!--添加-->
    <el-dialog
      :title="moduleTitle"
      :visible.sync="dialogFormVisible"
      width="600px">
      <el-form
        :model="form"
        label-width="50px">
        <el-form-item label="标题">
          <el-input
            v-model.lazy="form.title"
            clearable
            placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="详情">
          <el-input
            v-model="form.detail"
            clearable
            placeholder="请输入"
            type="textarea"/>
        </el-form-item>
        <el-form-item label="索引">
          <el-input
            v-model="form.index"
            type="number"
            clearable
            placeholder="请输入(数字越小越靠前)"/>
        </el-form-item>
        <el-form-item label="跳转">
          <el-input
            v-model="form.switchUrl"
            clearable
            placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="图片">
          <w-image-up
            v-if="dialogFormVisible"
            ref="UP_IMAGE"
            :souce-url="form.pic"/>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer">
        <el-button
          size="mini"
          @click="cancelBtnClick">取 消</el-button>
        <el-button
          :loading="loading"
          :disabled="!form.title"
          type="primary"
          size="mini"
          @click="okBtnClick">确 定</el-button>
      </div>
    </el-dialog>
    <w-viewer ref="WViewer"/>
  </div>
</template>

<script>
import BannerModel from '@/models/web/BannerModel'
import WImageUp from '@/components/global/imageUp/oss'
import WViewer from '@/components/global/viewer/index'
import WCell from '@/components/pc/Cell/cell_01'
export default {
  name: 'AdminWebBanner',
  components: {
    WImageUp,
    WViewer,
    WCell
  },
  data() {
    return {
      /**
       * 是否显示弹窗
       */
      dialogFormVisible: false,
      /**
       * 弹窗from
       */
      form: new BannerModel(),
      /**
       * 数据数组
       */
      dataArr: [],
      /**
       * 是否是编辑模式
       */
      isEdit: false,
      /**
       * 是否加载中
       */
      loading: false
    }
  },
  computed: {
    /**
     * 模态框标题
     * @returns {string}
     */
    moduleTitle() {
      return this.isEdit ? '编辑Banner' : '添加Banner'
    }
  },
  mounted() {
    this.getAllBanners()
  },
  methods: {
    /**
     * 添加按钮点击
     */
    addBtnClick() {
      this.isEdit = false
      this.form = new BannerModel()
      this.dialogFormVisible = true
    },
    /**
     * 确定按钮点击
     * @returns {Promise<void>}
     */
    async okBtnClick() {
      try {
        this.loading = true
        this.form.pic = await this.$refs['UP_IMAGE'].upImage()
        await this.form.addOrUpdate()
        this.dialogFormVisible = false
        this.getAllBanners()
      } catch (e) {
        this.$w_error_pop(e)
      } finally {
        this.loading = false
      }
    },
    /**
     * 取消按钮点击
     */
    cancelBtnClick() {
      this.dialogFormVisible = false
    },
    /**
     * 获取所有数据
     */
    getAllBanners() {
      BannerModel.loadAllModels().w_then((dataArr) => {
        this.dataArr = dataArr
      })
    },
    /**
     * 编辑按钮点击
     * @param index
     * @param row
     */
    editBtnClick(item) {
      this.isEdit = true
      this.form = new BannerModel()
      Object.assign(this.form, item)
      this.dialogFormVisible = true
    },
    /**
     * 删除按钮点击
     * @param item
     */
    detailBtnClick(item) {
      item.dustbin_push('title').w_then(() => {
        this.getAllBanners()
      })
    },
    /**
     * 图片显示
     * @param src
     */
    imageShowClick(src) {
      this.$refs['WViewer'].show([src])
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
